/* locky2dbm.c - convert wldb to dbm.
 *
 * Copyright (C) 2008 Yuuichi Teranishi
 *
 * This file is derived from Stumbler, a program developed by iphone-wireless
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * published by the Free Software Foundation.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; If not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *//*
 */
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ndbm.h>

#define WLDB_RECORD_SIZE 16
#define WLDB_ID_SIZE 8
#define LOCKY_CODE_SIZE 8

#define DBM_BLOCK_SIZE 512
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

int locky_code_equals(code1, code2)
     char *code1;
     char *code2;
{
  int i;
  for (i = 0; i < LOCKY_CODE_SIZE; i++) {
    if (code1[i] != code2[i]) {
      return FALSE;
    }
  }
  return TRUE;
}

void convertToDBM (file, size)
     char *file;
     int size;
{
  char record[WLDB_RECORD_SIZE];
  char hashedID[WLDB_ID_SIZE];
  char lockyCode[LOCKY_CODE_SIZE];

  DBM *dbf;
  datum key, data;
  datum result;
  int read_size = 0;
  int count = 0;
  int mod_count = 0;
  int new_count = 0;
  int uc_count = 0;

  if ((dbf = dbm_open ("locky", O_RDWR | O_CREAT, 0700)) == NULL) {
    fprintf (stderr, "error opening db\n");
    exit(1);
  }
  FILE *fp = fopen (file, "r");
  int read = fread (record, sizeof(char), WLDB_RECORD_SIZE, fp);
  read_size = read;
  while (read == WLDB_RECORD_SIZE && read_size <= size) {
    int i = 0;
    for (i = 0; i < WLDB_ID_SIZE; i++) {
      hashedID[i] = record[i];
    }
    for (i = 0; i< LOCKY_CODE_SIZE; i++) {
      lockyCode[i] = record[WLDB_ID_SIZE + i];
    }
    key.dsize = WLDB_ID_SIZE;
    key.dptr = hashedID;
    data.dsize = LOCKY_CODE_SIZE;
    data.dptr = lockyCode;
    count++;
    // result.dptr is malloc-ed.
    result.dptr = NULL;
    result = dbm_fetch (dbf, key);
    if (result.dptr == NULL) {
      new_count++;
      dbm_store (dbf, key, data, DBM_INSERT);
    }
    else {
      if (!locky_code_equals(result.dptr, data.dptr)) {// exists but modified
	mod_count++;
	dbm_store (dbf, key, data, DBM_REPLACE);
      }
      else { // exists and unchanged
	uc_count++;
      }
      //free (result.dptr);
    }
    if (count % 1000 == 0) {
      fprintf (stdout, "%d entries, %d new, %d modified, %d unchanged\n",
	       count, new_count, mod_count, uc_count);
    }
    read = fread (record, sizeof(char), WLDB_RECORD_SIZE, fp);
    read_size += read;
  }
  dbm_close (dbf);
  fclose (fp);
}

int
main (argc, argv)
     int argc;
     char **argv;
{
  struct stat sbuf;
  int size1, size2;

  if (argc != 2 && argc != 3) {
    fprintf (stderr, "Usage: locky2dbm default.wldb [new-default.wldb]");
    exit(1);
  }
  stat (argv[1], &sbuf);
  size1 = sbuf.st_size;
  if (argc == 2) {
    convertToDBM(argv[1], size1);
  }
  if (argc == 3) {
    stat (argv[2], &sbuf);
    size2 = sbuf.st_size;
    convertToDBM(argv[2], size2 - size1);
  }
  exit(0);
}
